﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.Data;

public partial class admin_users : System.Web.UI.Page
{
    private CMyLink lnkDel;
    private CMyLink lnkNav;

    protected void Page_Load(object sender, EventArgs e)
    {
        lnkDel = new CMyLink();
        this.Controls.Add(lnkDel);
        lnkDel.Click += new EventHandler(lnkDel_Click);
        lnkNav = new CMyLink();
        this.Controls.Add(lnkNav);
        lnkNav.Click += new EventHandler(lnkNav_Click);

        if (!IsPostBack)
        {
            InitTable(1, 20);
        }
    }

    void lnkNav_Click(object sender, EventArgs e)
    {
        InitTable(int.Parse((e as CMyLinkEventArg).Parameter), 20);
    }

    void lnkDel_Click(object sender, EventArgs e)
    {
        string[] pars = (e as CMyLinkEventArg).Parameter.Split(new char[] { '/'});
        CAdmin admin = new CAdmin();
        if (admin.DeleteUser(int.Parse(pars[0])))
        {
            InitTable(int.Parse(pars[1]), 20);   
        }
        else
        {
            ScriptManager.RegisterClientScriptBlock(Page, upTable.GetType(), "", "alert('操作失败！');", true);
        }
    }

    private void InitTable(int page, int count)
    {
        CAdmin admin = new CAdmin();
        DataTable dt = admin.ReadUsers();
        if (null != dt)
        {
            int top = page * count;
            if (top > dt.Rows.Count)
            {
                top = dt.Rows.Count;
            }
            StringBuilder sb = new StringBuilder();
            int c = 0;
            for (int i = (page - 1) * count; i < top; i++)
            {
                if (c % 2 == 0)
                {
                    sb.Append("<tr>");
                }
                else
                {
                    sb.Append("<tr bgcolor=\"#f5f5f5\">");
                }
                sb.Append("<td>" + i.ToString() + "</td>");
                sb.Append("<td>" + dt.Rows[i]["username"].ToString() + "</td>");
                sb.Append("<td>" + dt.Rows[i]["userpwd"].ToString() + "</td>");
                sb.Append("<td>" + (dt.Rows[i]["userrole"].ToString() == "0" ? "管理员" : "普通用户") + "</td>");
                sb.Append("<td>" + dt.Rows[i]["createtime"].ToString() + "</td>");
                sb.Append("<td>");
                sb.Append("<a href=\"" + Page.ClientScript.GetPostBackClientHyperlink(lnkDel, dt.Rows[i]["id"].ToString() + "/" + page.ToString()) + "\" onclick=\"return confirm('确认要删除吗?');\">删除</a>&nbsp;&nbsp;");
                sb.Append("<a target=\"_blank\" href=\"user_edit.aspx?id=" + dt.Rows[i]["id"].ToString() + "\">编辑</a>");
                sb.Append("</td>");
                sb.Append("</tr>");
                c++;
            }
            this.ltlTable.Text = sb.ToString();
            InitNav(page, count, dt.Rows.Count);
            this.upTable.Update();
        }
    }

    private void InitNav(int page, int count, int totalcount)
    {
        int pcount = totalcount / count;
        int reminder = totalcount % count;
        if (reminder > 0)
        {
            pcount++;
        }
        if (pcount == 0)
        {
            pcount++;
        }
        StringBuilder sb = new StringBuilder();        
        sb.Append("<dt class=\"current\">" + page.ToString() + "</dt>");
        int ltmp = 0;
        int rtmp = 0;
        for (int i = 1; i < 4; i++)
        {
            if (page - i >= 1)
            {
                sb.Insert(0, "<dt><a href=\"" + Page.ClientScript.GetPostBackClientHyperlink(lnkNav, (page - i).ToString()) + "\">" + (page - i).ToString() + "</a></dt>");
                ltmp++;
            }
            if (page + i <= pcount)
            {
                sb.Append("<dt><a href=\"" + Page.ClientScript.GetPostBackClientHyperlink(lnkNav, (page + i).ToString()) + "\">" + (page + i).ToString() + "</a></dt>");
                rtmp++;
            }
        }
        if (ltmp + rtmp < 6)
        {
            int left = ltmp + 1;
            int right = rtmp + 1;
            if (page - 3 > 1)
            {
                for (int i = left; i <= 6 - rtmp; i++)
                {
                    if (page - i >= 1)
                    {
                        sb.Insert(0, "<dt><a href=\"" + Page.ClientScript.GetPostBackClientHyperlink(lnkNav, (page - i).ToString()) + "\">" + (page - i).ToString() + "</a></dt>");
                        ltmp++;
                    }
                    else
                    {
                        break;
                    }
                }
            }
            else if (page + 3 < pcount)
            {
                for (int i = right; i <= 6 - ltmp; i++)
                {
                    if (page + i <= pcount)
                    {
                        sb.Append("<dt><a href=\"" + Page.ClientScript.GetPostBackClientHyperlink(lnkNav, (page + i).ToString()) + "\">" + (page + i).ToString() + "</a></dt>");
                        rtmp++;
                    }
                    else
                    {
                        break;
                    }
                }
            }
        }
        if (page - ltmp > 1)
        {
            sb.Insert(0, "<dt><a href=\"" + Page.ClientScript.GetPostBackClientHyperlink(lnkNav, (page - ltmp - 1).ToString()) + "\">...</a></dt>");
        }
        //首页、前一页
        if (page > 1)
        {
            sb.Insert(0, "<dt><a href=\"" + Page.ClientScript.GetPostBackClientHyperlink(lnkNav, (page - 1).ToString()) + "\">|&lt;</a></dt>");
            sb.Insert(0, "<dt><a href=\"" + Page.ClientScript.GetPostBackClientHyperlink(lnkNav, "1") + "\">|&lt;&lt;</a></dt>");
        }
        else
        {
            sb.Insert(0, "<dt>|&lt;</dt>");
            sb.Insert(0, "<dt>|&lt;&lt;</dt>");
        }

        if (page + rtmp < pcount)
        {
            sb.Append("<dt><a href=\"" + Page.ClientScript.GetPostBackClientHyperlink(lnkNav, (page + rtmp + 1).ToString()) + "\">...</a></dt>");
        }
        //末页、下一页
        if (page < pcount)
        {
            sb.Append("<dt><a href=\"" + Page.ClientScript.GetPostBackClientHyperlink(lnkNav, (page + 1).ToString()) + "\">&gt;|</a></dt>");
            sb.Append("<dt><a href=\"" + Page.ClientScript.GetPostBackClientHyperlink(lnkNav, pcount.ToString()) + "\">&gt;&gt;|</a></dt>");
        }
        else
        {
            sb.Append("<dt>&gt;|</dt>");
            sb.Append("<dt>&gt;&gt;|</dt>");
        }
        sb.Insert(0, "<dl id=\"nav\">");
        sb.Append("</dl>");
        this.ltlNav.Text = sb.ToString();
    }
}